/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */


Ext.app.App = function(cfg){
	//Ext.Msg.alert('',this.toString);
	Ext.apply(this, cfg);
	this.addEvents({//自定义事件
		'ready' : true,
		'beforeunload' : true
	});
	Ext.onReady(this.initApp, this);
};

Ext.extend(Ext.app.App, Ext.util.Observable, {
    isReady: false,
    startMenu: null,
    modules: null,

    initApp : function(){
		
    	this.startConfig = this.startConfig || this.getStartConfig();

        this.desktop = new Ext.Desktop(this);

		this.launcher = this.desktop.taskbar.startMenu; //开始菜单
		
		
		//加载开始程序树
		this.getModules();

//		this.modules = this.getModules();
//        if(this.modules){
//            this.initModules(this.modules);
//        }

        this.init();

        Ext.EventManager.on(window, 'beforeunload', this.onUnload, this);//给window注册事件
		
		this.fireEvent('ready', this);
        this.isReady = true;
    },

    getModules : Ext.emptyFn,
    init : Ext.emptyFn,

    initModules : function(ms){
		for(var i = 0, len = ms.length; i < len; i++){
            var m = ms[i];

			if(!m.launcher.noStart) {
				this.launcher.add(m.launcher);
			}
            
			//on('contextmenu'
            m.app = this;
        }
    },

    getModule : function(name){
    	var ms = this.modules;
    	for(var i = 0, len = ms.length; i < len; i++){
			if(ms[i].launcher.windowId == name) {
				return ms[i].launcher;
			}else if(ms[i].launcher.menu && ms[i].launcher.menu.items) {
				for(var j=0,items = ms[i].launcher.menu.items;j<items.length;j++) {
					if(items[j].windowId == name) {
						return items[j];
					}
				}
			}
    		//if(ms[i].id == name || ms[i].appType == name){
    			//return ms[i];
			//}
        }
/*
		for(var i = 0, len = ms.length; i < len; i++){
    		if(ms[i].id == name || ms[i].appType == name){
    			return ms[i];
			}
        }*/
        return '';
    },
	
	importMod:function(url,callback){
		Ext.Ajax.request({
			url: 'jsClass/'+url,//js文件名
			method: 'GET',
			callback:function(request, success, response){//request请求所调用的参数 success请求是否成功 response返回数据
				if(!success) {
					alert(c.error,c.alertMsg.UnknownError);
				}
				var headerDom = document.getElementsByTagName('head').item(0);
				if(headerDom.getElementsByTagName("script").length==1){
					headerDom.removeChild(headerDom.getElementsByTagName("script")[0]);
				}
				var jsDom = document.createElement('script');
				jsDom.type = 'text/javascript';
				jsDom.language = 'javascript';
				jsDom.id = url;
				jsDom.defer = "defer";
				jsDom.text = response.responseText;
				headerDom.appendChild(jsDom);
				callback();
			}
			
		});
	},
	ajax: function(url, args, callback){
//		var conn = new Ext.data.Connection({ //建立连接实例
//			autoAbort:true,//链接是否会自动断开
//			//defaultHeaders:{referer:'http://127.0.0.1'},//请求的默认首部信息
//			disableCaching:true,//请求是否会禁用缓存
//			extraParams:{userid:1},//请求的额外参数
//			method:"POST",
//			timeout:300,//连接的超时时间
//			url:url
//		});
//		//在创建了conn之后，可以调用request()函数发送请求，处理返回的结果
//	
//		conn.request({  
//			success: function(response) {  
//				Ext.Msg.alert('info', response.responseText);  
//			},  
//			failure: function() {  
//				Ext.Msg.alert('warn', 'failure');
//			} 
//		});  

		args = args ? args : {};
		Ext.Ajax.request({
			url: url,
			params: Ext.urlEncode(args),
			method: 'POST',
			callback: function(request, success, response) {
				if (!success) {
					alert(c.error,c.alertMsg.UnknownError);
					return;
				}
				callback(response.responseText);
			}
		});
	},
	onReady : function(fn, scope){
		
        if(!this.isReady){
            this.on('ready', fn, scope);
        }else{
            fn.call(scope, this);
        }
    },

    getDesktop : function(){
        return this.desktop;
    },

    onUnload : function(e){
        if(this.fireEvent('beforeunload', this) === false){
            e.stopEvent();
        }
    }
});